/**
 * 本文件用于处理各种状态
 */

import { ref, reactive } from 'vue';

// 这个状态用于控制全局的加载状态
export const loading = ref(false);

// Toast 的状态数据
export const toast_data = reactive({
    show: false,
    message: '',
    icon: '',
});

const toast_icon = {
    success: '/icon/toast/success.svg',   // 绿色 打勾
    warning: '/icon/toast/warning.svg',   // 红色 感叹号
    toast: '/icon/toast/prompt.svg',      // 蓝色 字母 i
    clock: '/icon/toast/clock.svg',       // 紫红色 时钟
};

export const toast = {
    // 公共的定时关闭 - 未来要拆开多个
    common: null,
    success(msg, delay=1000) {
        // 清除定时器
        if (toast.common) {
            clearTimeout(toast.common);
        }
        // 修改状态
        toast_data.show = true;
        toast_data.message = msg;
        toast_data.icon = toast_icon.success;
        // 定时关闭
        toast.common = setTimeout(() => {
            toast_data.show = false;
            toast.common = null;
        }, delay);
    },
    warning(msg, delay=1000) {
        // 清除定时器
        if (toast.common) {
            clearTimeout(toast.common);
        }
        // 修改状态
        toast_data.show = true;
        toast_data.message = msg;
        toast_data.icon = toast_icon.warning;
        // 定时关闭
        toast.common = setTimeout(() => {
            toast_data.show = false;
            toast.common = null;
        }, delay);
    },
    toast(msg, delay=1000) {
        // 清除定时器
        if (toast.common) {
            clearTimeout(toast.common);
        }
        // 修改状态
        toast_data.show = true;
        toast_data.message = msg;
        toast_data.icon = toast_icon.toast;
        // 定时关闭
        toast.common = setTimeout(() => {
            toast_data.show = false;
            toast.common = null;
        }, delay);
    },
    clock(msg, delay=1000) {
        // 清除定时器
        if (toast.common) {
            clearTimeout(toast.common);
        }
        // 修改状态
        toast_data.show = true;
        toast_data.message = msg;
        toast_data.icon = toast_icon.clock;
        // 定时关闭
        toast.common = setTimeout(() => {
            toast_data.show = false;
            toast.common = null;
        }, delay);
    },
};